/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package genetic;

/**
 *
 * @author skyr
 */
public class Solution {

    private Double cost;
    private Short[][] solutionArray;
    private String binaryRepresentation;
    private int rows;
    private int columns;
    private int id;

    /**
     * recomended calculates cost of solution in creation
     *
     * @param solutionArray
     * @param i
     * @param j
     * @param costArray
     */
    public Solution(Short[][] solutionArray, int i, int j, Integer[][] costArray) {
        this.solutionArray = solutionArray;
        fitnessFunction(costArray);
        id = GeneralController.getNewId();
    }

    public final Double fitnessFunction(Integer[][] costArray) {
        cost = 0D;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                cost = cost + (costArray[i][j] * solutionArray[i][j]);
            }
        }
        return cost;
    }

    private String convertSolutionToBinaryString(int phges, int sygkentrotes, Short[][] solution) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < phges; i++) {
            for (int j = 0; j < sygkentrotes; j++) {
                stringBuilder.append(solution[i][j]);
            }
        }
        return stringBuilder.toString();
    }

    public String getBinaryRepresentation() {
        if (binaryRepresentation == null) {
            binaryRepresentation = convertSolutionToBinaryString(rows, columns, solutionArray);
        }
        return binaryRepresentation;
    }

    public void setBinaryRepresentation(String binaryRepresentation) {
        this.binaryRepresentation = binaryRepresentation;
    }

    public int getColumns() {
        return columns;
    }

    public void setColumns(int columns) {
        this.columns = columns;
    }

    public Double getCost() {
        return cost;
    }

    public void setCost(Double cost) {
        this.cost = cost;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Short[][] getSolutionArray() {
        return solutionArray;
    }

    public void setSolutionArray(Short[][] solutionArray) {
        this.solutionArray = solutionArray;
    }

    public int getId() {
        return id;
    }}
